<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <th:block th:include="common/common_head :: commonHeader('发票预制')"/>
    <link th:href="@{/static/css/plugins/cropper/cropper.min.css}" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight" style="padding-bottom: 0px;">
	<div class="row">
		<form class="form-horizontal" method="post" id="dataForm">
		<div class="form-group" style="margin-bottom:10px">
		    <div class="col-sm-8">
			     <input class="btn btn-primary" type="button" onclick="makeInvoice()" value="生成预制发票" />
			     <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取 消</button>
		    </div>
		</div>
		<div id="basicInfo">
			<div class="ibox float-e-margins">
			    <div class="ibox-content">
			        <div class="form-group">
			            <label class="col-sm-1 control-label">发票类型：</label>
			            <div class="col-sm-3">
			                <select class="form-control" id="invoiceTypeSel" name="invoiceType" sysDict="FPLX" emptyFirst="true" required="true" fieldType="发票类型"></select>
			            </div>
			            <label class="col-sm-1 control-label">发票号码：</label>
			            <div class="col-sm-3">
			            	<input name="invoiceNo" type="text" placeholder="发票号码" class="form-control" required="true" fieldType="发票号码" />
			            </div>
			            <label class="col-sm-1 control-label">开票日期：</label>
			            <div class="col-sm-3">
			                <input type="text" id="dateKpId" name="dateKp" placeholder="开票日期" fieldType="开票日期" class="form-control" doneEvent="calPayDate" isDate="true" readonly="readonly" required="true" />
			            </div>
			        </div>
			        <div class="form-group">
						<!-- 付款日期基于开票日期和付款条件 -->
			            <label class="col-sm-1 control-label">付款日期：</label>
			            <div class="col-sm-3">
			                <input name="dateFk" id="dateFkId"  type="text" placeholder="付款日期" class="form-control" required="true" fieldType="付款日期" />
			            </div>
			            <label class="col-sm-1 control-label">凭证抬头文本：</label>
			            <div class="col-sm-3">
			                <input name="bkTxt" type="text" placeholder="凭证抬头文本" class="form-control" />
			            </div>
			            <label class="col-sm-1 control-label">备注：</label>
			            <div class="col-sm-3">
			                <input name="remark" id="remarkId" type="text" placeholder="备注" class="form-control" />
			            </div>
			        </div>
			        <div class="form-group">
			            <label class="col-sm-1 control-label">对账单号：</label>
			            <div class="col-sm-3">
			                <input type="text" placeholder="对账单号" name="billNo" class="form-control" readOnly="readOnly" th:value="${invoiceHeader.billNo}" />
			            </div>
			             <label class="col-sm-1 control-label">发票方：</label>
			             <div class="col-sm-3">
			                <input type="text" placeholder="发票方" class="form-control" readOnly="readOnly" th:value="${invoiceHeader.vendorName}" />
			                <input name="invoicePerson" type="hidden" th:value= "${invoiceHeader.vendorCode}" />
			             </div>
						<label class="col-sm-1 control-label">过账日期：</label>
						<div class="col-sm-3">
							<input type="text" name="dateGz" placeholder="过账日期" isDate="true" class="form-control" th:value= "${#dates.format(invoiceHeader.DateGz,'yyyy-MM-dd')}" />
						</div>
			        </div>
					<div class="form-group">
					    <label class="col-sm-1 control-label">付款条件：</label>
					    <div class="col-sm-3">
					    	<input type="text" placeholder="付款条件" id="payConditionText" class="form-control" readOnly="readOnly" th:value="${invoiceHeader.payConditionText}" />
					    	<input name="payCondition" type="hidden" th:value= "${invoiceHeader.payCondition}" />
					    </div>
					   	<label class="col-sm-1 control-label">发票总额：</label>
					   	<div class="col-sm-3">
					      	<input type="text" placeholder="发票总额" name="sumMoney" id="sumMoneyId" readOnly="readOnly" class="form-control" th:value="${invoiceHeader.sumMoney}" />
					   	</div>
					   	<label class="col-sm-1 control-label">税代码：</label>
					   	<div class="col-sm-3">
					      	<input id="taxCodeText" type="text" placeholder="税代码" class="form-control" readOnly="readOnly" th:value="${invoiceHeader.taxCodeText}" />
					      	<input name="taxCode" type="hidden" th:value="${invoiceHeader.taxCode}" />
					   	</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">税额：</label>
						<div class="col-sm-3">
							<input name="taxMoney" id="taxMoneyId" type="text" placeholder="税额" class="form-control" readonly="readonly" />
						</div>
						<label class="col-sm-1 control-label">货币码：</label>
						<div class="col-sm-3">
							<input type="text" placeholder="货币码" class="form-control" readOnly="readOnly" th:value="${invoiceHeader.currencyText}" />
							<input name="currency" type="hidden" th:value="${invoiceHeader.currency}" />
						</div>
						<label class="col-sm-1 control-label"></label>
						<div class="col-sm-3"></div>
					</div>
			    </div>
			</div>
		</div>
		</form>
	</div>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<!-- Chosen -->
<script th:src="@{/static/js/plugins/chosen/chosen.jquery.js}"></script>

<!-- 文件上传的function -->
<script type="text/javascript">
	$(function () {
		initDictDataSel();
		initDateText("isDate");
		calTaxMoney();
	});
	
	/**
	** 计算付款日期
	**/
	function calPayDate(dates) {
		var params = {
			"kpDateStr":dates,
			"payCondtionText":$("#payConditionText").val()
		};
	    Page.ajaxGET(ctxPath +"/invoice/calPayDate", params, function(o) {
	        if(o.code == 0) {
	        	$("#dateFkId").val(o.result);
	        } else {
	        	Dialog.alert(o.msg);
	        }
		}, {"loadingMsg" : "正在与服务器通讯付款日期……"});
	}
	
	/**
	** 计算税额
	**/
	function calTaxMoney() {
		var params = {
			"billMonoey":$("#sumMoneyId").val(),
			"taxCodeText":$("#taxCodeText").val()
		};
	    Page.ajaxGET(ctxPath +"/invoice/calTaxMoney", params, function(o) {
	        if(o.code == 0) {
	        	$("#taxMoneyId").val(o.result);
	        } else {
	        	Dialog.alert(o.msg);
	        }
		}, {"loadingMsg" : "正在与服务器通讯计算税额……"});
	}
	
	/**
	** 生成预制发票
	**/
	function makeInvoice() {
		var hasEmptyField = false;
		var requiredInps = $("#dataForm").find("input[required='true'],select[required='true']");
		requiredInps.each(function(i, e) {
			let _e = $(e);
			if(_e.val().trim() == "") {
				Dialog.alert(_e.attr("fieldType") + "不能为空！");
				hasEmptyField = true;
				return false;
			}
		});
		if(hasEmptyField) {return;}
		var data = serializeForm("dataForm", {'ignoreReadOnly':false, 'ignoreDisabled':false});
	    Page.ajaxPOST(ctxPath +"/invoice/genInvoices", data, function(o) {
	        if(o.code == 0) {
	        	Dialog.msg("生成预制发票成功!");
	        	parent.refreshHeader();
	        	Dialog.closeFrame(window.name);
	        } else {
	        	Dialog.alert("生成预制发票失败!");
	        }
		}, {"loadingMsg" : "正在往服务器提交数据……"});
	}
</script>
</body>
</html>
